#!/usr/bin/zsh
# We need to use zsh for 'ulimit -e'
. /etc/utils.sh

cd "$(dirname "$0")"
export LD_LIBRARY_PATH=$PWD:$PWD/lib

export LANG=en_US.UTF-8
export SIEGEHOME=/app/sage/skelmir

if [ "$1" = "client" ]; then
  MODE=client
  PATHS_CACHE=/rw/sage
  CONNECT_STRING="-connect $2"
  RUN=sagetvclient
  PROPFILE=/rw/sage/SageClient.properties
else
  MODE=server
  PATHS_CACHE=/var/media
  CONNECT_STRING="x"
  RUN=sagetv
  PROPFILE=/rw/sage/Sage.properties
fi

if runnable java; then
  JVM=java
else
  JVM=./skelmir/siege
fi


# Read a given key from Sage.properties, printing the value to stdout.
readarg()
{
  local name="$1" out= d= zzz=
  for d in Sage.properties Sage.properties.autobackup; do
    [ -e "/rw/sage/$d" ] || continue
    while IFS== read key value; do
      [ "$key" = "$name" ] && out=$value
      [ "$key" = "zzz" ] && zzz=1  # magic zzz key indicating a complete file
    done <"/rw/sage/$d"
    [ -z "$zzz" ] || break
  done
  echo "$out"
}


ulimit -c 49152

if [ "$MODE" = "server" ]; then
  start_sagesrv
  start_adsmgr
  start_adscale

  # Cleanup any Oregano files if they switched
  /app/oregano/dogcatcher.sh &

  # Start up channel service for dogfood, but only if off_the_grid is set.
  if [ "$(readarg off_the_grid)" = "true" ]; then
    echo "Running chsrv." >&2
    babysit 60 ./chsrv | logos chsrv &
  fi
fi

SIEGEOPT=
[ -e /tmp/disablejit ] && SIEGEOPT="$SIEGEOPT -Xint"

mkdir -p /rw/sage
hnvram -qr 1ST_SERIAL_NUMBER >/tmp/serial

# Fix permissions for running SageTV as non-root
chmod 775 /rw/sage
chown video.video /rw/sage
chown video.video /rw/sage/*
if [ "$MODE" = "server" ]; then
  mkdir -p /rw/lucene
  mkdir -p /rw/lucene/person
  mkdir -p /rw/lucene/show
  chown video.video /var/media/Wiz.bak
  chown video.video /rw/lucene
  chown video.video /rw/lucene/*
  chown video.video /rw/lucene/person/*
  chown video.video /rw/lucene/show/*
  chown video.video /var/media/tv/*.properties
fi

WEBSERVERJARS=ant.jar:core-3.1.1.jar:jetty-6.1.19.jar:jetty-ajp-6.1.19.jar:jetty-rewrite-handler-6.1.19.jar:jetty-sslengine-6.1.19.jar:jetty-starter.jar:jetty-util-6.1.19.jar:jsp-2.1.jar:jsp-2.1-jetty-6.1.19.jar:jsp-api-2.1.jar:sagex-api.jar:servlet-api-2.5-6.1.19.jar
MOBILEAPIJARS=gftv_mobile_api_deploy.jar:libjson.jar
ANALYTICSJARS=gfiber_analytics_sage_plugin_deploy.jar
MIGRATIONJARS=gftv_spicerack_migration_deploy.jar
GFIBERJARS=$MOBILEAPIJARS:$ANALYTICSJARS:$MIGRATIONJARS
if [ "$MODE" = "client" ]; then
  GFIBERJARS=$ANALYTICSJARS
fi

(
  cat Sage.properties.defaults.base
  [ "$MODE" = "server" ] && cat Sage.properties.defaults.webserver
  [ "$MODE" = "server" ] && cat Sage.properties.defaults.mobileapi
  [ "$MODE" = "server" ] && cat Sage.properties.defaults.migration
  cat SageClient.properties.defaults.analytics
) >/tmp/Sage.properties.defaults

BOXTYPE=`cat /etc/platform`
if [ "GFHD100" = "$BOXTYPE" ] ; then
  echo "fiber/miracast/enable=true" >> /tmp/Sage.properties.defaults
fi

read junk1 memtotal junk2 </proc/meminfo
echo "System memory: $memtotal kb" >&2
if [ "$memtotal" -le 524288 ]; then
  # Mostly for mindspeed eval board which has only 512M
  HEAPSIZE=-Xmx300m
elif is-tv-box; then
  # Restrict JVM heap size for TV Boxes since 2.0 only has 512M for Linux
  HEAPSIZE=-Xmx384m
else
  HEAPSIZE=-Xmx512m
fi

# Tell siege not to dump its entire heap when signals happen
export SIEGEDISABLEHEAPDUMP=1

# Tell siege not to launch gdb to debug itself when signals happen
export SIEGENOGDB=1

# Prevent siege-on-arm from exiting on changes to stdin
export SIEGE_BLOCK_GETNEXTEVENT=1

# Disable JIT on methods that are crashing the VM
export SIEGE_JIT_EXCLUDE=/app/sage/jitexcludes

PREKILL_OPTIONS=
if [ -e /tmp/DEBUG ]; then
  PREKILL_OPTIONS="-S 11 -T 15"
fi

# Allow the JVM to increase its thread priorities above normal
ulimit -e 40

babysit 10 alivemonitor $PREKILL_OPTIONS \
    /tmp/sagetvalive 300 60 600 setuid video "$JVM" \
    -Djava.io.tmpdir=/tmp \
    -Djava.library.path=.:./lib \
    -Dsage.paths.logs=/tmp/log \
    -Dsage.embedded=1 \
    -Dsage.platform=$BOXTYPE \
    -Dsage.paths.cache="$PATHS_CACHE" \
    -Djava.lang.runtime.exec.vfork \
    -Djava.lang.system.gc.force \
    -Xss256k \
    -cp Sage.jar:vecmath.jar:skelmir/Libraries/Apache.jar:jcifs.jar:jce-jdk13-150.jar:apache.jar:log4j-1.2.17.jar:jrestubs.jar:lucene-core-3.6.0.jar:$WEBSERVERJARS:$GFIBERJARS \
    $HEAPSIZE \
    -verbose:gc \
    $SIEGEOPT \
    sage.Sage 0 0 "$CONNECT_STRING" \
    "$RUN $PROPFILE"
